package io.gitee.zhucan123.core.infrastructure.dataconversion.serializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.niimbot.middlend.core.config.JsonConfiguration;
import com.niimbot.middlend.core.infrastructure.dataconversion.EnumDeserialize;
import com.niimbot.middlend.core.infrastructure.util.DateHandler;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author: zhuCan
 * @date: 2020/2/25 8:50
 * @description:
 */
public class LocalDateDeserializer extends JsonDeserializer<LocalDateTime> {


  @Override
  public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
    if (!StringUtils.isBlank(jsonParser.getText())) {
      if (EnumDeserialize.isInteger(jsonParser.getText())){
        return DateHandler.millisToLocal(Long.parseLong(jsonParser.getText()));
      }
      return LocalDateTime.from(DateTimeFormatter.ofPattern(JsonConfiguration.PATTERN).parse(jsonParser.getText()));
    }

    return null;
  }

}
